## --*-- coding:utf-8 --*--

import warnings

warnings.filterwarnings("ignore")
import requests
import random
import time
import datetime


class PortraitCrowd():
	''' 人群画像类'''

	# 定义属性
	cateId = None
	end_time = (str(datetime.datetime.now() - datetime.timedelta(days=1)).split(' ')[0])
	start_time = (str(datetime.datetime.now() - datetime.timedelta(days=30)).split(' ')[0])
	dateRange = start_time + '|' + end_time
	cookies = None
	headers = {
		'Host': 'sycm.taobao.com',
		'Connection': 'keep-alive',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
		'Accept': '*/*',
		'Referer': 'https://sycm.taobao.com/mc/mq/search_customer?cateFlag=1&cateId=50010788&dateRange=2018-11-08%7C2018-11-08&dateType=day&device=0&parentCateId=0&spm=a21ag.11815228.LeftMenu.d1484.7e6c50a5IMs27c&selfCustomerId=%E6%B4%97%E9%9D%A2%E5%A5%B6',
		'Accept-Encoding': 'gzip, deflate, br',
		'Accept-Language': 'zh-CN,zh;q=0.9'
	}


	def GetKeyword(self, itemId):

		''' 通过商品id获取访客数最大的10个关键词 '''


		# 取出关键词数据
		url = 'https://sycm.taobao.com/flow/v3/new/item/source/detail.json'
		data = {
			'dateRange': self.dateRange,
			'dateType': 'recent30',
			'pageSize': '10',
			'page': '1',
			'order': 'desc',
			'orderBy': 'uv',
			'itemId': itemId,
			'device': '2',
			'pageId': '23.s1150',
			'pPageId': '23',
			'pageLevel': '2',
			'childPageType': 'se_keyword',
			'belong': 'all',
			'indexCode': 'uv,crtByrCnt,crtRate',
			'_': '1536734494730',
			'token': '1a998269c'
		}
		# 无线
		TenKeyword = []
		wx_resp = requests.get(url, headers=self.headers, cookies=self.cookies, verify=False, params=data)
		if wx_resp.json()['code'] == 0:
			KeyWordData = wx_resp.json()['data']['data']
			for  i in KeyWordData:
				if i['pageName']['value'] != '其他':
					TenKeyword.append(i['pageName']['value'])
			if len(TenKeyword) != 10:
				data['page'] = '2'
				wx_resp = requests.get(url, headers=self.headers, cookies=self.cookies, verify=False, params=data)
				TenKeyword.append(wx_resp.json()['data']['data'][0]['pageName']['value'])

			return TenKeyword



			# for k in wx_resp.json()['data']['data']:
			# 	TenKeyword.append(k['pageName']['value'])
			# return TenKeyword


		elif wx_resp.json()['code'] == 5810:
			return wx_resp.json()

		elif wx_resp.json()['code'] == 1100:
			return wx_resp.json()

		
	def GetClass(self):
		''' 获取类目 '''

		url = 'https://sycm.taobao.com/mc/common/getShopCate.json'
		payload = {
			'edition': 'pro,vip',
			'_': '1541750717339',
			'token': ''
		}
		resp = requests.get(url, params=payload, cookies=self.cookies, verify=False, headers=self.headers)
		return resp.json()  # {"traceId":"0b0adda915417513932655099ea736","code":0,"data":[[0,50010788,"彩妆\/香水\/美妆工具",1,"pro","N",50010788,"彩妆\/香水\/美妆工具"]],"message":"操作成功"}

	def AttributePortrait(self, seKeyword):
		''' 属性画像 '''
		word_data_list = []
		word_dict = {}

		# dateRange = self.start_time + '|' + self.end_time
		# dateRange = '2018-10-15|2018-11-13'

		url = 'https://sycm.taobao.com/mc/mkt/searchPortrait/popularity.json'
		payload = {
			'cateId': self.cateId,  # 传入的类目ID
			'seKeyword': seKeyword + '||',  # 搜索关键词
			'device': '0',
			'indexCode': 'payPopularity',  # 搜索人气 搜索人数占比 点击人气 点击人数占比 点击率 交易指数 支付转化率
			'dateType': 'recent30',
			'dateRange': self.dateRange,
			'_': '1541751475532',
			'token': ''
		}

		''' 性别分布分布 '''
		payload['attrType'] = 'gender'
		resp = requests.get(url, params=payload, cookies=self.cookies, verify=False, headers=self.headers)  #{'message': '活动期间系统压力较大，数据更新时间暂缓，请您稍后再试，给您带来不便，敬请谅解！', 'traceId': '0b0baec315422461678641992edef5', 'code': 1009}
		if resp.json()['code'] == 1009:
			day1 = 2
			day2 = 31
			while True:
				self.end_time = (str(datetime.datetime.now() - datetime.timedelta(days=day1)).split(' ')[0])
				self.start_time = (str(datetime.datetime.now() - datetime.timedelta(days=day2)).split(' ')[0])
				self.dateRange = self.start_time + '|' + self.end_time
				payload['dateRange'] = self.dateRange
				resp = requests.get(url, params = payload, cookies=self.cookies, verify=False, headers=self.headers)
				if resp.json()['code'] == 1009:
					if day1 < 10: # 数据往后延迟10天，10天还没数据就退出循环
						day1 += 1
						day2 += 1
						time.sleep(random.uniform(0.5, 1))
					else:
						break
				elif resp.json()['code'] == 0:
					break
		keyword = list(resp.json()['data'].keys())[0]
		word_dict['keyword'] = keyword
		if resp.json()['data'][keyword]:
			gender = resp.json()['data'][keyword]['dataList']
			word_dict['gender'] = gender
		time.sleep(random.uniform(0.5, 1))

		# ''' 年龄分布 '''
		payload['attrType'] = 'age'
		resp = requests.get(url, params=payload, cookies=self.cookies, verify=False, headers=self.headers)
		# print(resp.json())
		keyword = list(resp.json()['data'].keys())[0]
		if resp.json()['data'][keyword]:
			age = resp.json()['data'][keyword]['dataList']
			word_dict['age'] = age
		time.sleep(random.uniform(0.5, 1))

		# ''' 省份分布 '''
		# payload['attrType'] = 'prov'
		# resp = requests.get(url, params=payload, cookies=self.cookies, verify=False, headers=self.headers)
		# # print(resp.json())
		# keyword = list(resp.json()['data'].keys())[0]
		# if resp.json()['data'][keyword]:
		#     provdata = resp.json()['data'][keyword]['dataList']
		#     word_dict['province'] = provdata
		# time.sleep(random.uniform(0.5, 1))

		''' 城市分布 '''
		payload['attrType'] = 'city'
		resp = requests.get(url, params=payload, cookies=self.cookies, verify=False, headers=self.headers)
		time.sleep(random.uniform(0.5, 1))
		# print(resp.json())
		keyword = list(resp.json()['data'].keys())[0]
		if resp.json()['data'][keyword]:
			city = resp.json()['data'][keyword]['dataList']
			word_dict['city'] = city

			word_data_list.append(word_dict)
		return word_data_list

	def mian(self, TenKeyword):
		if type(TenKeyword) == list:
			sum_data_list = []
			for se_keyword in TenKeyword:
				time.sleep(random.uniform(2, 3))
				crawler_data = self.AttributePortrait(se_keyword)
				if crawler_data:
					sum_data_list.append(crawler_data)

			return sum_data_list
		elif type(TenKeyword) == dict:
			return TenKeyword







# if __name__ == '__main__':
# 	PortraitCrowd = PortraitCrowd()

# 	# 未过期
# 	PortraitCrowd.cookies =  {"uc1":"cookie14=UoTYNciEsob/mQ==&lng=zh_CN","sn":"lukezyh:wang","_mw_us_time_":"1543304521072","tracknick":"","cna":"15ZiFBhnImkCAcFwhT7OdcJz","apush9b2c167680966f700967779e7d37d994":'{"ts":1543304538900,"parentId":1543304522878}',"_tb_token_":"eeeee7e51e6eb","cookie2":"174414bb9c49eeda0ac094eaf974ada8","isg":"BAkJZNE0tkQisUqw7m4RJxBzFDxjVv2IUkXXK6t-hfAv8ikE86YNWPewMtjEsZXA","t":"d22ee415076b3161af56be547ea3931c","_m_h5_tk":"e8cff60a244ee92c339569b667e20754_1543311723180","uc3":"id2=&nk2=&lg2=","x":"22184377","unb":"4225955827","_m_h5_tk_enc":"35b98c8a01d53cbe17de2ed866b4ef2f","thw":"cn","csg":"41fa7c60","v":"0","skt":"8414bcdca8eb7b53"}

# 	# 过期
# 	# PortraitCrowd.cookies = {
# 	# 	"apushd2dce1c5dd89f161d480bff397a6184e": "%7B%22ts%22%3A1539756202271%2C%22parentId%22%3A1539756180225%7D",
# 	# 	"v": "0",
# 	# 	"sg": "%E5%BA%9702",
# 	# 	"unb": "3397628220",
# 	# 	"csg": "0551b63d",
# 	# 	"mt": "np=",
# 	# 	"tg": "0",
# 	# 	"existShop": "MTUzOTc1NjE3Ng%3D%3D",
# 	# 	"cna": "yT1EFFgLEWoCAcFwhT6Vrka3",
# 	# 	"t": "a10741e33487142bf87560d9c07ddccc",
# 	# 	"_l_g_": "Ug%3D%3D",
# 	# 	"dnk": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
# 	# 	"lgc": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
# 	# 	"cookie17": "UNN0nnQ5PzBj6g%3D%3D",
# 	# 	"cookie2": "1567caec42e1101d1a81f7bf42d39236",
# 	# 	"uc1": "cookie16=W5iHLLyFPlMGbLDwA%2BdvAGZqLg%3D%3D&tmb=1&cookie21=URm48syIZxx%2F&cookie15=URm48syIIVrSKA%3D%3D&existShop=true&pas=0&cookie14=UoTfItcJAi%2BEVw%3D%3D&tag=8&lng=zh_CN",
# 	# 	"_nk_": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
# 	# 	"_cc_": "URm48syIZQ%3D%3D",
# 	# 	"JSESSIONID": "25E1427542CF79285F49E7AE31C16318",
# 	# 	"_tb_token_": "f77b3683e5be8",
# 	# 	"cookie1": "UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D",
# 	# 	"uc3": "vt3=F8dByRmsjdJMVDCjcCQ%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=W5iHLLyFOGW7aA%3D%3D",
# 	# 	"isg": "BPb2Pz7csYdsX0VjQCUOhFp2SyX4FzpRuVSYgmDf4ll0o5Y9yKeKYVzRv_HPEDJp",
# 	# 	"tracknick": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
# 	# 	"skt": "1a3fa92b43f4db73"
# 	# }

# 	print(PortraitCrowd.GetClass())

# 	# itemId = '567812452645'
# 	# TenKeyword = PortraitCrowd.GetKeyword(itemId)

# 	# PortraitCrowd.cateId = '50020611'
# 	# print(PortraitCrowd.mian(TenKeyword))  #['其他']
